package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CustomerDao;
import cn.gjculture.shopping.api.dao.OrderDao;
import cn.gjculture.shopping.api.dao.PromotionDao;
import cn.gjculture.shopping.api.dao.PromotionMapper;
import cn.gjculture.shopping.api.entity.Customer;
import cn.gjculture.shopping.api.entity.Promotion;
import cn.gjculture.shopping.api.entity.PromotionTree;
import cn.gjculture.shopping.api.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    PromotionDao promotionDao;

    @Autowired
    PromotionMapper promotionMapper;

    @Autowired
    OrderDao orderDao;
    @Autowired
    CustomerDao customerDao;

    @Override
    public List<Promotion> queryByCustomerId(String customerId) {
        List<Promotion> promotions= promotionMapper.queryByCustomerId(customerId);
        List<Promotion> promotionList=new ArrayList<>();
        if(promotions!=null&& promotions.size()>0){
            promotionList.addAll(promotions);
            List<Promotion> promotionList1= getNext(promotions);
            if(promotionList1!=null&&promotionList1.size()>0){
                promotionList.addAll(promotionList1);
            }
        }
        // 查询消费记录
        for(Promotion p: promotionList){
            if(orderDao.queryCountBuyByCustomerId(p.getCustomerId())>0){
                p.setBuy(true);
            }
        }
        return promotionList;
    }

    @Override
    public List<PromotionTree> queryForTree(String customerId) {
        List<PromotionTree> list=new ArrayList<>();
        PromotionTree root=new PromotionTree();
        list.add(root);
        Customer customer=customerDao.queryById(customerId);
        this.setPromotionTreeName(root,customer);
        this.getChildren(root,customerId);

        return list;
    }

    private void setPromotionTreeName(PromotionTree root, Customer customer) {
        if(customer.getLevel()>2){
            root.setName(customer.getNickname()+"(VIP)");
        }else {
            root.setName(customer.getNickname());
        }
    }

    private void getChildren(PromotionTree root, String customerId) {
        List<Promotion> promotions= promotionMapper.queryByCustomerId(customerId);
        if (promotions!=null){
            root.setValue(promotions.size());
            List<PromotionTree> leafs=new ArrayList<>(promotions.size());
            int consumerNum=0;
            for(Promotion p:promotions){
                PromotionTree leaf=new PromotionTree();
                this.setPromotionTreeName(leaf,p.getCustomer());
                leafs.add(leaf);
                if(orderDao.queryCountBuyByCustomerId(p.getCustomerId())>0){
                    consumerNum++;
                }
                this.getChildren(leaf,p.getCustomerId());
            }
            root.setConsumerNum(consumerNum);
            root.setChildren(leafs);
        }
    }

    private List<Promotion> getNext(List<Promotion> promotions) {
        List<Promotion> promotionList=new ArrayList<>();
        for(Promotion p:promotions){
            List<Promotion> promotionList1=promotionMapper.queryByCustomerId(p.getCustomerId());
            if(promotionList1!=null&& promotionList1.size()>0){
                promotionList.addAll(promotionList1);
                promotionList.addAll(getNext(promotionList1));
            }
        }
        return promotionList;
    }


}
